package com.ciwong.mobilelib.widget.clipimage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.RelativeLayout;

/**
 * 头像剪切布局
 * 
 * @author res-dlzhou
 */
public class ClipImageLayout extends RelativeLayout implements
        ClipImageBorderView.IClipBorderChange
{
    private ClipZoomImageView mZoomImageView;

    private ClipImageBorderView mClipImageView;

    public ClipImageLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        mZoomImageView = new ClipZoomImageView(context);
        mClipImageView = new ClipImageBorderView(context);
    }

    public void setNeedCrop(boolean crop)
    {
        mClipImageView.setNeedScale(crop);
    }

    /**
     * 加载图片，并设置剪切图片的大小
     * 
     */
    public void setDrawable(Drawable drawable, int clip_width, int clip_heigth)
    {
        android.view.ViewGroup.LayoutParams lp = new LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT);

        mZoomImageView.setImageDrawable(drawable);
        this.addView(mZoomImageView, lp);
        this.addView(mClipImageView, lp);

        // 计算padding的px
        clip_width = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, clip_width, getResources()
                        .getDisplayMetrics());
        clip_heigth = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, clip_heigth, getResources()
                        .getDisplayMetrics());
        mZoomImageView.initClipFrameSize(clip_width, clip_heigth);
        mClipImageView.initClipFrameSize(clip_width, clip_heigth);
        mClipImageView.setIClipBorderChange(this);
    }

    public void turnLeft()
    {
        mZoomImageView.turnLeft();
    }

    public void turnRight()
    {
        mZoomImageView.turnRight();
    }

    /**
     * 裁切图片
     * 
     * @return
     */
    public Bitmap getCropImage()
    {
        return mZoomImageView.clip();
    }

    @Override
    public void change(int horizontalPadding, int verticalPadding, int width,
            int height)
    {
        mZoomImageView.initClipFrameSize(width, height);
        mZoomImageView.initClipPadding(verticalPadding, horizontalPadding);
    }
}
